/**
 * Description:  数据抽取类
 * Author:    EX-WANGHUA001 
 * Filename:    DataExtractServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年8月11日
 */
package com.paic.mhis.hcpms.audit.biz.service.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.hcpms.audit.biz.service.ProblemDataExtractService;
import com.paic.mhis.hcpms.audit.biz.service.RandomCodeService;
import com.paic.mhis.hcpms.audit.dao.ProblemDataDao;
import com.paic.mhis.hcpms.audit.util.EventCodeUtil;

@Service("problemDataExtractService")
public class ProblemDataExtractServiceImpl implements ProblemDataExtractService {
	@Resource(name = "problemDataDao")
	private ProblemDataDao problemDataDao;

	@Autowired
	private RandomCodeService randomCodeService;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void extractProblemData() {
		try {
			Calendar calendar = Calendar.getInstance();
			int year = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH) + 1;
			// 抽取事件数据
			List<Map<String, String>> problemData = problemDataDao
					.queryProblemData();
			for (Map<String, String> tempMap : problemData) {
				// 获取序列
				String targetType = tempMap.get("targetType");
				if (null == targetType || "".equals(targetType)) {
					targetType = "1";
				}
				String caseCode = tempMap.get("caseCode");
				// String warnNoBiz = tempMap.get("warnNoBiz");
				// String targetCode = tempMap.get("targetCode");
				String warnNo = tempMap.get("warnNo");
				Map<String, String> paramsMap = new HashMap<String, String>();
				// paramsMap.put("targetType", targetType);
				paramsMap.put("caseCode", caseCode);
				// paramsMap.put("targetCode", targetCode);
				// 根据caseCode查询已经插入的条数
				int caseCount = problemDataDao.queryCaseCount(paramsMap);
				if (caseCount > 0) {
					continue;
				}
				tempMap.put("targetType", targetType);
				// String seq = problemDataDao.getSequence();
				String problemCode = randomCodeService.getCode(
						EventCodeUtil.event_codeSJ,
						EventCodeUtil.industry_type1);
				if (StringUtils.isEmpty(problemCode)) {
					problemCode = randomCodeService.getCode(
							EventCodeUtil.event_codeSJ,
							EventCodeUtil.industry_type1);
				}

				tempMap.put("problemCode", problemCode);
				// 导出事件信息
				problemDataDao.insertProblemData(tempMap);
				List<Map<String, String>> targetData = problemDataDao
						.queryTargetData(warnNo);
				for (Map<String, String> target : targetData) {
					String tagetType = target.get("targetType");
					target.put("targetType", tagetType.substring(1));
					target.put("problemCode", problemCode);
					// 导出事件对象信息
					problemDataDao.insertProblemTargetData(target);

				}

			}
			// 数据状态变更为已处理
			problemDataDao.updateProblemData();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		String str = "01";
		System.out.println(str.substring(1));
	}

}
